package com.leecode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Leet155 {
	public static void main(String[] args) {
		MinStack minStack = new MinStack();
		minStack.push(-2);
		minStack.push(0);
		minStack.push(-3);
		System.out.println(minStack.getMin());
		minStack.pop();
		System.out.println(minStack.top());
		System.out.println(minStack.getMin());

	}

}
class MinStack {
	int min=0;
	List<Integer> list=new ArrayList();
	List<Integer> minList=new ArrayList();
	boolean flag=true;
	// List<Integer> minList=new ArrayList();

	/** initialize your data structure here. */
	public MinStack() {

	}

	public void push(int x) {
		this.list.add(x);
		this.minList.add(x);
		Collections.sort(this.minList);
	}

	public void pop() {
		// if(this.list.size()!=0)
		this.list.remove(list.size()-1);
		this.minList=new ArrayList<>(this.list);//bug,不写this,之后的minlist就是空的?!!!
//		this.minList=new ArrayList<>(list.size());//bug,不写this,之后的minlist就是空的?!!!
//		Collections.copy(this.minList,this.list);
		Collections.sort(this.minList);
	}

	public int top() {
		// if(this.list.size()!=0)
		return this.list.get(list.size()-1);
	}

	public int getMin() {
		return this.minList.get(0);
	}
}
